package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.InterviewStateDto;
import com.returnsoft.resource.dto.OjtStateDto;
import com.returnsoft.resource.dto.TrainingStateDto;
import com.returnsoft.resource.eao.TrainingStateEao;
import com.returnsoft.resource.entity.InterviewState;
import com.returnsoft.resource.entity.OjtState;
import com.returnsoft.resource.entity.TrainingState;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class TrainingStateBean
 */
@Stateless
@LocalBean
public class TrainingStateBean implements TrainingStateInterface {

	@EJB
	private TrainingStateEao eao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public TrainingStateBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<TrainingStateDto> findAll() throws RecruitmentException {
		try {
			List<TrainingState> entities = eao.findAll();
			List<TrainingStateDto> dtos = new ArrayList<TrainingStateDto>();
			for (TrainingState entity : entities) {
				TrainingStateDto dto = conv.fromTrainingState(entity);
				dtos.add(dto);
			}
			return dtos;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}
	
	@Override
	public List<TrainingStateDto> findIsPending() throws RecruitmentException {
		try {
			List<TrainingState> entities = eao.findIsPending();
			List<TrainingStateDto> dtos = new ArrayList<TrainingStateDto>();
			for (TrainingState entity : entities) {
				TrainingStateDto dto = conv.fromTrainingState(entity);
				dtos.add(dto);
			}
			return dtos;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}
	
	@Override
	public TrainingStateDto findById(Integer stateId)
			throws RecruitmentException {
		try {
			TrainingState state = eao.findById(stateId);
			TrainingStateDto stateDto = conv.fromTrainingState(state);
			
			return stateDto;
			
		} catch (EaoExcepcion e) {
   			throw new RecruitmentException(e.getMessage());
   		} catch (ConversionExcepcion e) {
   			throw new RecruitmentException(e.getMessage());
   		} catch (Exception e) {
   			e.printStackTrace();
   			throw new RecruitmentException(e.getMessage());
   		}
		
	}
	

}
